﻿using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using InfiniteHopper.Types;

namespace InfiniteHopper
{
    /// <summary>
    ///This script handles player selection. You can navigate through the available players using buttons. The currently
    ///selected player is shown on screen and recorded in the PlayerPrefs record, then later retrieved in the game
    /// </summary>
    public class IPHPlayerSelection : MonoBehaviour
    {
        //A list of players that can be unlocked with tokens
        public PlayerUnlock[] playerList;

        //The currently selected player
        public int currentPlayer = 0;

        //The player prefs record name of the current player. It's where we save the current player so we can load it in the level
        public string playerPrefsName = "CurrentPlayer";


        // 金币
        internal int tokens = 0;
        public string tokensPrefsName = "Tokens";

        internal int index = 0;

        //The icon that displays the number of tokens needed to unlock a character
        public Transform tokenIcon;

        public Text tokenValue;


        public Button btn_left;
        public Button btn_right;

        void Start()
        {
            tokens = PlayerPrefs.GetInt(tokensPrefsName, tokens);
            tokenValue.text = tokens.ToString();
            currentPlayer = PlayerPrefs.GetInt(playerPrefsName, currentPlayer);
            SetPlayer(currentPlayer);

            btn_left.onClick.AddListener(() => ChangePlayer(-1));   
            btn_right.onClick.AddListener(() => ChangePlayer(1));
        }


        /// <summary>
        /// 切换玩家
        /// </summary>
        /// <param name="changeValue"></param>
        void ChangePlayer(int changeValue)
        {
            try
            {
                Debug.Log("Change player by: " + changeValue);
                currentPlayer += changeValue;
                if (currentPlayer > playerList.Length - 1) currentPlayer = 0;
                if (currentPlayer < 0) currentPlayer = playerList.Length - 1;
                SetPlayer(currentPlayer);
            }
            catch (System.Exception e)
            {
                Debug.LogError(e.Message);
            }
        }

        //This function activates the selected player, while deactivating all the others
        void SetPlayer(int playerNumber)
        {
            tokens = PlayerPrefs.GetInt(tokensPrefsName, tokens);
            tokenValue.text = tokens.ToString();

            for (index = 0; index < playerList.Length; index++)
            {
                if (index != playerNumber) playerList[index].playerIcon.gameObject.SetActive(false);
                else playerList[index].playerIcon.gameObject.SetActive(true);
            }
            SpriteRenderer[] playerParts = playerList[playerNumber].playerIcon.GetComponentsInChildren<SpriteRenderer>();
            // 当前金币足够
            if (tokens >= playerList[playerNumber].tokensToUnlock)
            {
                foreach (SpriteRenderer part in playerParts) part.color = new Color(part.color.r, part.color.g, part.color.b, 1);
                if (tokenIcon)
                {
                    tokenIcon.gameObject.SetActive(false);
                }
                PlayerPrefs.SetInt(playerPrefsName, playerNumber);
                Debug.Log("Current player set to: " + playerNumber);
                PlayerPrefs.SetInt("CharactersUnlocked", playerNumber);
            }
            else
            {
                foreach (SpriteRenderer part in playerParts) part.color = new Color(part.color.r, part.color.g, part.color.b, 0.3f);
                if (tokenIcon)
                {
                    tokenIcon.gameObject.SetActive(true);
                    tokenIcon.Find("Text").GetComponent<Text>().text = (playerList[playerNumber].tokensToUnlock - tokens).ToString();
                }
            }
        }
    }
}
